﻿(function () {
    NSFile("Gianduia.AppKit.NSPopUpButton");
    NSImport("Gianduia.AppKit.NSInput");
    NSClass("Gianduia.AppKit.NSPopUpButton").Extends("Gianduia.AppKit.NSInput").Implements({
        _optionTemplate: function () {
            if (!this.constructor.prototype.__optionTemplate) {
                this.constructor.prototype.__optionTemplate = document.createElement("option");
                this.constructor.prototype._optionTemplate = function () {
                    return this.__optionTemplate
                }
            } return this.__optionTemplate
        }, elementTagName: function () {
            return "select"
        }, acceptsFirstResponder: function () {
            return true
        }, mouseDown: function (A) {
            Gianduia.Foundation.NSNotificationCenter.defaultCenter.addObserver(this, "elementSelectionOnChange", "change", this.element());
            this.performAction(A)
        }, keyDown: function (A) {
            Gianduia.Foundation.NSNotificationCenter.defaultCenter.addObserver(this, "elementSelectionOnChange", "change", this.element());
            this.performAction(A)
        }, elementSelectionOnChange: function (A) {
            this.setSelectedIndex(this.indexOfSelectedItem());
            if (this._selectedIndex < 0) {
                this.setSelectedObject(null)
            } else {
                this.setSelectedObject(this._contentObjects[this._selectedIndex]);
                this.removeTitleIfNeeded()
            }
        }, draw: function () {
            this._super("draw")();
            if (this._hasContentObjectsChanges) {
                this.populateContentObjectsIntoDOM();
                this._hasContentObjectsChanges = false
            } this.updateSelectedObjectIntoDOM()
        }, didChangeValueForKey: function (B, A, C) {
            this._super("didChangeValueForKey")(B, A, C);
            switch (B) {
                case "title": this.updateTitle(C);
                    break
            }
        }, content: NSProperty(), hasContentObjectsChanges: NSProperty(), contentObjects: NSProperty({
            setterImplementation: function (A) {
                this._hasContentObjectsChanges = true;
                this._contentObjects = A;
                this.setNeedsDisplay(true)
            }
        }), setContentValues: function (A) {
            this._contentObjects = A;
            this.setNeedsDisplay(true)
        }, contentValues: function () {
            return this._contentObjects
        }, selectedIndex: NSProperty({
            setterImplementation: function (A) {
                if (!this._updatingBindings) {
                    this.updateBindingsByIndex(A)
                } this._selectedIndex = A
            }
        }), selectedObject: function () {
            return this._selectedObject
        }, setSelectedObject: function (A) {
            if (!this._updatingBindings) {
                this.updateBindingsByObject(A)
            } this._selectedObject = A
        }, hideTitleOnSelect: NSProperty({
            defaultValue: false
        }), updateSelectedObject: function () {
            if (this._selectedBy == "object") {
                this.updateBindingsByObject(this._selectedObject)
            } else {
                this.updateBindingsByIndex(this._selectedIndex)
            }
        }, _updatingBindings: false, updateBindingsByIndex: function (A) {
            this._updatingBindings = true;
            if (A) {
                this._selectedIndex = A;
                if (this._contentObjects) {
                    if (this._contentObjects[A]) {
                        this.setSelectedObject(this._contentObjects[A])
                    }
                }
            } this._selectedBy = "index";
            this.setNeedsDisplay(true);
            this._updatingBindings = false
        }, updateBindingsByObject: function (B) {
            this._updatingBindings = true;
            if (!B) {
                if (this.hasTitle()) {
                    this.setSelectedIndex(-1)
                } else {
                    this.setSelectedIndex(0)
                }
            } else {
                if (this._contentObjects) {
                    var A = this._contentObjects.indexOfObject(B);
                    this.setSelectedIndex(A)
                } else {
                    this.setSelectedIndex(0)
                }
            } this._selectedObject = B;
            this._selectedBy = "object";
            this.setNeedsDisplay(true);
            this._updatingBindings = false
        }, updateSelectedObjectIntoDOM: function () {
            if (typeofthis._selectedIndex == "number") {
                var A = this._selectedIndex;
                if (this.hasTitle()) {
                    A += 1
                } if (this.element().childNodes[A]) {
                    this.element().childNodes[A].selected = true
                }
            }
        }, populateContentObjectsIntoDOM: function () {
            this.removeAllItems();
            if (this._contentObjects) {
                this.addItemsWithObjects(this._contentObjects)
            } this.updateSelectedObject()
        }, hasTitle: function () {
            return (this._ownerComponentDeclaration.bindingNamed("title") != null) && (!this.titleRemoved())
        }, canShowTitle: function () {
            return (this._title != undefined) && (this.titleRemoved())
        }, canHideTitle: function () {
            return (this._title != undefined) && (!this.titleRemoved())
        }, removeTitleIfNeeded: function (A) {
            if (this.hasTitle() && this.hideTitleOnSelect()) {
                this.removeItemAtIndex(0);
                this.setTitleRemoved(true)
            }
        }, titleFormatter: NSProperty(), title: NSProperty(), _titleNode: null, titleRemoved: NSProperty({
            defaultValue: false
        }), updateTitle: function (A) {
            if (this._titleNode != null) {
                this._titleNode.replaceContents(document.createTextNode(A))
            }
        }, addItemWithTitle: function (D, C, A) {
            var B = this._optionTemplate().cloneNode(true);
            B.value = C ? C : "";
            B.appendChild(document.createTextNode(D));
            if (!A) {
                A = this._element
            } A.appendChild(B);
            return B
        }, titleKeyPath: NSProperty({
            defaultValue: "description"
        }), valueKeyPath: NSProperty({
            defaultValue: "description"
        }), enabledKeyPath: NSProperty(), addItemsWithObjects: function (E) {
            if (!E) {
                return
            } var A = document.createDocumentFragment(), B, G, C, F, D;
            if (this.hasTitle()) {
                this._titleNode = this.addItemWithTitle(this._title, "", A)
            } for (B = 0, G = E.length;
			B < G; B++) {
                C = (B === this._selectedIndex);
                F = E[B].valueForKeyPath(this.titleKeyPath());
                if (this._titleFormatter) {
                    F = this._titleFormatter.stringForObjectValue(F)
                } D = this.addItemWithTitle(F, E[B].valueForKeyPath(this.valueKeyPath()), A);
                if (C) {
                    D.setAttribute("selected", true)
                } if (this.enabledKeyPath() && !E[B].valueForKeyPath(this.enabledKeyPath())) {
                    D.setAttribute("disabled", "disabled")
                }
            } this._element.appendChild(A)
        }, addItemsWithTitles: function (C) {
            var A, B;
            for (A = 0, B = C.length; A < B; A++) {
                this.addItemWithTitle(C[A])
            }
        }, insertItemWithTitleAtIndex: function (D, A) {
            var B = document.createElement("option"), C = this.element();
            B.value = D;
            B.appendChild(document.createTextNode(D));
            C.insertBefore(B, C.childNodes[A]);
            this.synchronizeTitleAndSelectedItem()
        }, removeAllItems: function () {
            var B = this.element(), A;
            if (!B) {
                return
            } for (A = B.childNodes.length - 1; A >= 0; A--) {
                B.removeChild(B.childNodes[A])
            } this.synchronizeTitleAndSelectedItem()
        }, removeItemWithTitle: function (C) {
            var B = this.element(), A;
            for (A = B.childNodes.length - 1; A >= 0; A--) {
                if (B.childNodes[A].value == C) {
                    B.removeChild(B.childNodes[A]);
                    this.synchronizeTitleAndSelectedItem();
                    break
                }
            }
        }, removeItemAtIndex: function (A) {
            var B = this.element();
            B.removeChild(B.childNodes[A])
        }, selectedItem: function () {
            return this.titleOfSelectedItem()
        }, titleOfSelectedItem: function () {
            if (this.element().selectedIndex) {
                return this.element().childNodes[this.element().selectedIndex].value
            } return null
        }, indexOfSelectedItem: function () {
            if (this.element().selectedIndex < 0) {
                return null
            } else {
                var A = this.element().selectedIndex;
                if (this.hasTitle()) {
                    A -= 1
                } return A
            }
        }, objectValue: function () {
            return this.indexOfSelectedItem()
        }, hideTitle: function (A) {
            if (this.canHideTitle()) {
                this.removeItemAtIndex(0);
                this.setTitleRemoved(true)
            }
        }, showTitle: function (A) {
            if (this.canShowTitle()) {
                this.insertItemWithTitleAtIndex(this._title, "", 0);
                this.setTitleRemoved(false)
            }
        }, selectItem: function (A) {
            this._selectedIndex = A;
            this.element().childNodes[this._selectedIndex].selected = true
        }, synchronizeTitleAndSelectedItem: function () {
        }, insertTemplate: function () {
            var A = this._super("insertTemplate")();
            this.addItemsWithObjects(this._contentObjects);
            return A
        }
    })
})();

(function () { 
    NSClass("Gianduia.AppKit.NSPopUpButton").Extends("Gianduia.AppKit.NSInput").Implements();
})();
